unit main;
(*
Copyright (c) 2013, Matthew Hipkin <http://www.matthewhipkin.co.uk>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, LCLIntF, LCLType, IntfGraphics, Spin, Menus, fpImage, IniFiles
  {$IFNDEF Windows}, xlib, xkblib, XKB{$ENDIF};

type

  { TfrmMain }

  TfrmMain = class(TForm)
    btnSave: TButton;
    checkAutoHide: TCheckBox;
    btnColourOn: TColorButton;
    btnColourOff: TColorButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    menuExit: TMenuItem;
    trayMenu: TPopupMenu;
    textInterval: TSpinEdit;
    timerMain: TTimer;
    trayIcon: TTrayIcon;
    procedure btnSaveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure menuExitClick(Sender: TObject);
    procedure timerMainTimer(Sender: TObject);
    procedure trayIconDblClick(Sender: TObject);
  private
    { private declarations }
    capsOn: Boolean;
    scrollOn: Boolean;
    numOn: Boolean;
    procedure setStatus;
    procedure loadConfig;
    procedure saveConfig;
  public
    { public declarations }
    appdir: String;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.lfm}

function HexToTColor(sColor: string): TColor;
begin
  Result :=
    RGBToColor(
      { get red value }
      StrToInt( '$'+Copy( sColor, 2, 2 ) ),
      { get green value }
      StrToInt( '$'+Copy( sColor, 4, 2 ) ),
      { get blue value }
      StrToInt( '$'+Copy( sColor, 6, 2 ) )
    );
end;

function ColourToHTML(inCol: TColor): String;
var
  i: Integer;
  S: String;
begin
  i := Integer(inCol);
  S := '#';
  S := S + copy(IntToHex(i, 7), 6, 2);
  S := S + copy(IntToHex(i, 7), 4, 2);
  S := S + copy(IntToHex(i, 7), 2, 2);
  Result := S;
end;

{ TfrmMain }

procedure TfrmMain.loadConfig;
var
  conf: TIniFile;
begin
  conf := TIniFile.Create(appdir + 'traytog.ini');
  btnColourOff.ButtonColor := HexToTColor(conf.ReadString('program','offcolour','#FF0000'));
  btnColourOn.ButtonColor := HexToTColor(conf.ReadString('program','oncolour','#00FF00'));
  conf.Free;
end;

procedure TfrmMain.saveConfig;
var
  conf: TIniFile;
begin
  conf := TIniFile.Create(appdir + 'traytog.ini');
  conf.WriteString('program','offcolour',ColourToHTML(btnColourOff.ButtonColor));
  conf.WriteString('program','oncolour',ColourToHTML(btnColourOn.ButtonColor));
  conf.Free;
end;

procedure TfrmMain.setStatus;
{$IFNDEF Windows}
var
  d: PDisplay;
  n: PWord;
{$ENDIF}
begin
  {$IFNDEF Windows}
  d := XOpenDisplay(nil);
  if (d = nil) then
    exit;
  XkbGetIndicatorState(d,XkbUseCoreKbd,@n);
  if (QWORD(n) and $01) = $01 then capsOn := true
  else capsOn := false;
  if (QWORD(n) and $02) = $02 then numOn := true
  else numOn := false;
  if (QWORD(n) and $04) = $04 then scrollOn := true
  else scrollOn := false;
  XCloseDisplay(d);
  {$ELSE}
  if Odd(GetKeyState(VK_CAPITAL)) then capsOn := true
  else capsOn := false;
  if Odd(GetKeyState(VK_NUMLOCK)) then numOn := true
  else numOn := false;
  if Odd(GetKeyState(VK_SCROLL)) then scrollOn := true
  else scrollOn := false;
  {$ENDIF}
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
//  setStatus;
//  timerMain.Interval := 1000;
  appdir :=  GetUserDir + '.traytog' + PathDelim;
  if not DirectoryExists(appdir) then mkdir(appdir);
  loadConfig;
end;

procedure TfrmMain.btnSaveClick(Sender: TObject);
begin
  saveConfig;
  frmMain.Hide;
end;

procedure TfrmMain.Label4Click(Sender: TObject);
begin
  OpenURL('http://www.matthewhipkin.co.uk');
end;

procedure TfrmMain.menuExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TfrmMain.timerMainTimer(Sender: TObject);
var
  TempBitmap: TBitmap;
  hLines: TStrings;
begin
  hLines := TStringList.Create;
  timerMain.Interval := 200;
  setStatus;
  TempBitmap := TBitmap.Create;
  TempBitmap.Width := 16;
  TempBitmap.Height := 16;
  TempBitmap.Canvas.Brush.Style := bsSolid;
  if capsOn then
  begin
    TempBitmap.Canvas.Brush.Color := btnColourOn.ButtonColor;
    hLines.Add('Caps Lock: On');
  end
  else TempBitmap.Canvas.Brush.Color := btnColourOff.ButtonColor;
  TempBitmap.Canvas.Rectangle(0,0,5,15);
  if numOn then
  begin
    TempBitmap.Canvas.Brush.Color := btnColourOn.ButtonColor;
    hLines.Add('Num Lock: On');
  end
  else TempBitmap.Canvas.Brush.Color := btnColourOff.ButtonColor;
  TempBitmap.Canvas.Rectangle(5,0,10,15);
  if scrollOn then
  begin
    TempBitmap.Canvas.Brush.Color := btnColourOn.ButtonColor;
    hLines.Add('Scroll Lock: On');
  end
  else TempBitmap.Canvas.Brush.Color := btnColourOff.ButtonColor;
  TempBitmap.Canvas.Rectangle(10,0,15,15);
  trayIcon.Icon.Assign(TempBitmap);
  TempBitmap.Free;
  trayIcon.Hint := hLines.Text;
  hLines.Free;
end;

procedure TfrmMain.trayIconDblClick(Sender: TObject);
begin
  frmMain.Show;
end;

end.

